<!-- 动态UI渲染器 -->
<template>
  <view class="ui-renderer">
    <!-- 轮播图区域 -->
    <s-home-navbar 
      v-if="getSection('navbar')?.enabled"
      :config="getSection('navbar')?.config"
      :data="getSection('navbar')?.data"
    />
    
    <!-- 轮播图区域 -->
    <view 
      v-if="getSection('banner')?.enabled"
      class="banner-section"
      :style="getSection('banner')?.config"
    >
      <swiper
        :indicator-dots="getSection('banner')?.config?.indicatorDots"
        :indicator-color="getSection('banner')?.config?.indicatorColor"
        :indicator-active-color="getSection('banner')?.config?.indicatorActiveColor"
        :autoplay="getSection('banner')?.config?.autoplay"
        :interval="getSection('banner')?.config?.interval"
        :duration="getSection('banner')?.config?.duration"
        :circular="getSection('banner')?.config?.circular"
        class="banner-swiper"
        @change="onBannerChange"
      >
        <swiper-item 
          v-for="(item, index) in getSection('banner')?.data?.items" 
          :key="item.id"
          class="banner-item"
          @tap="onBannerClick(item)"
        >
          <image 
            class="banner-image" 
            :src="item.imageUrl || placeholderImage" 
            mode="aspectFill"
            @error="onImageError"
          />
          <view v-if="item.title" class="banner-overlay">
            <text class="banner-title">{{ item.title }}</text>
            <text v-if="item.subtitle" class="banner-subtitle">{{ item.subtitle }}</text>
          </view>
        </swiper-item>
      </swiper>
      
      <!-- 自定义指示器 -->
      <view class="custom-indicators">
        <view 
          v-for="(item, index) in getSection('banner')?.data?.items" 
          :key="index"
          class="indicator-dot"
          :class="{ active: currentBannerIndex === index }"
        ></view>
      </view>
    </view>

    <!-- 搜索框 -->
    <view 
      v-if="getSection('searchBox')?.enabled"
      class="search-box"
      :style="getSection('searchBox')?.config"
      @tap="onSearchClick"
    >
      <text class="search-placeholder">{{ getSection('searchBox')?.config?.placeholder }}</text>
      <text class="search-icon">🔍</text>
    </view>

    <!-- 菜单区域 -->
    <s-home-menu 
      v-if="getSection('menu')?.enabled"
      :config="getSection('menu')?.config"
      :data="getSection('menu')?.data"
      @itemClick="onMenuClick"
    />

    <!-- 广告魔方区域 -->
    <s-home-ad-cube 
      v-if="getSection('adCube')?.enabled"
      :config="getSection('adCube')?.config"
      :data="getSection('adCube')?.data"
      @adClick="onAdClick"
    />

    <!-- 精选内容区域 -->
    <s-home-featured 
      v-if="getSection('featured')?.enabled"
      :config="getSection('featured')?.config"
      :data="getSection('featured')?.data"
      @itemClick="onFeaturedClick"
      @moreClick="onMoreClick"
    />
  </view>
</template>

<script setup>
  import { ref, computed, onMounted } from 'vue';
  import sheep from '@/sheep';
  import SHomeNavbar from '@/sheep/components/s-home-navbar/s-home-navbar.vue';
  import SHomeMenu from '@/sheep/components/s-home-menu/s-home-menu.vue';
  import SHomeAdCube from '@/sheep/components/s-home-ad-cube/s-home-ad-cube.vue';
  import SHomeFeatured from '@/sheep/components/s-home-featured/s-home-featured.vue';

  const props = defineProps({
    config: {
      type: Object,
      default: () => ({})
    }
  });

  const emits = defineEmits(['sectionClick', 'imageUpload', 'configUpdate']);

  const currentBannerIndex = ref(0);
  const placeholderImage = 'https://via.placeholder.com/750x500?text=请上传图片';

  // 获取指定区域配置
  const getSection = (sectionId) => {
    return props.config.sections?.find(section => section.id === sectionId);
  };

  // 轮播图切换事件
  const onBannerChange = (e) => {
    currentBannerIndex.value = e.detail.current;
  };

  // 轮播图点击事件
  const onBannerClick = (item) => {
    emits('sectionClick', { type: 'banner', item });
    if (item.linkUrl) {
      sheep.$router.go(item.linkUrl);
    }
  };

  // 搜索框点击事件
  const onSearchClick = () => {
    const searchData = getSection('searchBox')?.data;
    emits('sectionClick', { type: 'search', data: searchData });
    if (searchData?.searchUrl) {
      sheep.$router.go(searchData.searchUrl);
    }
  };

  // 菜单点击事件
  const onMenuClick = (item) => {
    emits('sectionClick', { type: 'menu', item });
  };

  // 广告点击事件
  const onAdClick = (item) => {
    emits('sectionClick', { type: 'ad', item });
  };

  // 精选内容点击事件
  const onFeaturedClick = (item) => {
    emits('sectionClick', { type: 'featured', item });
  };

  // 更多按钮点击事件
  const onMoreClick = () => {
    emits('sectionClick', { type: 'more' });
  };

  // 图片加载错误处理
  const onImageError = (e) => {
    console.error('图片加载失败:', e);
  };

  // 上传图片
  const uploadImage = async (uploadKey, filePath) => {
    try {
      const response = await sheep.$api.home.uploadImage(filePath, uploadKey);
      if (response.code === 0) {
        // 更新配置中的图片URL
        updateImageUrl(uploadKey, response.data.url);
        emits('imageUpload', { uploadKey, url: response.data.url });
        return response.data.url;
      }
    } catch (error) {
      console.error('图片上传失败:', error);
      uni.showToast({
        title: '图片上传失败',
        icon: 'none'
      });
    }
  };

  // 更新图片URL
  const updateImageUrl = (uploadKey, url) => {
    // 这里需要更新配置中对应uploadKey的imageUrl
    // 具体实现需要根据配置结构来调整
    emits('configUpdate', { uploadKey, url });
  };

  // 暴露方法给父组件
  defineExpose({
    uploadImage,
    updateImageUrl
  });
</script>

<style lang="scss" scoped>
.ui-renderer {
  background: #f5f6fa;
  min-height: 100vh;
}

.banner-section {
  position: relative;
  height: 500rpx;

  .banner-swiper {
    width: 100%;
    height: 100%;

    .banner-item {
      position: relative;
      width: 100%;
      height: 100%;

      .banner-image {
        width: 100%;
        height: 100%;
      }

      .banner-overlay {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
        padding: 40rpx 30rpx 30rpx;

        .banner-title {
          display: block;
          color: #ffffff;
          font-size: 32rpx;
          font-weight: 600;
          margin-bottom: 8rpx;
        }

        .banner-subtitle {
          display: block;
          color: rgba(255, 255, 255, 0.8);
          font-size: 24rpx;
        }
      }
    }
  }

  .custom-indicators {
    position: absolute;
    bottom: 20rpx;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12rpx;

    .indicator-dot {
      width: 16rpx;
      height: 16rpx;
      border-radius: 50%;
      background: rgba(255, 255, 255, 0.5);
      transition: all 0.3s ease;

      &.active {
        background: #ff4757;
        transform: scale(1.2);
      }
    }
  }
}

.search-box {
  position: absolute;
  bottom: 20rpx;
  left: 20rpx;
  right: 20rpx;
  height: 80rpx;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 40rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx;
  backdrop-filter: blur(10rpx);

  .search-placeholder {
    color: #999999;
    font-size: 28rpx;
  }

  .search-icon {
    font-size: 32rpx;
    color: #999999;
  }
}
</style>

